package com.situ.hospital.servlet;

import com.situ.hospital.pojo.Restocking;
import com.situ.hospital.pojo.query.RestockingQuery;
import com.situ.hospital.pojo.vo.RestockingVO;
import com.situ.hospital.service.IRestockingService;
import com.situ.hospital.service.impl.RestockingServiceImpl;
import com.situ.hospital.util.JSONUtil;
import com.situ.hospital.util.MyBeanUtil;
import com.situ.hospital.util.PageResult;
import com.situ.hospital.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/restocking")
public class RestockingServlet extends HttpServlet {

    private IRestockingService restockingService = new RestockingServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.update");
        Restocking restocking = MyBeanUtil.copyToBean(req, Restocking.class);
        restockingService.update(restocking);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.selectById");
        String id = req.getParameter("id");
        Restocking restocking = restockingService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(restocking));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.add");
        Restocking restocking = MyBeanUtil.copyToBean(req, Restocking.class);
        restockingService.add(restocking);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        restockingService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.deleteById");
        String id = req.getParameter("id");
        restockingService.deleteById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RestockingServlet.selectByPage");
        RestockingQuery restockingQuery = MyBeanUtil.copyToBean(req, RestockingQuery.class);
        PageResult<RestockingVO> pageResult = restockingService.selectByPage(restockingQuery);
        JSONUtil.toJSON(resp, pageResult);
    }
}
